package com.lagou.springexp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

/**
 * @ClassName ServiceScannerConfigurer
 * @Description
 * @Author playboy
 * @Date 2021/6/5 5:24 下午
 * @Version 1.0
 **/
public class ServiceScannerConfigurer implements BeanDefinitionRegistryPostProcessor {
	private static Logger log = LoggerFactory.getLogger(ServiceScannerConfigurer.class);

	private String basePackage;

	private Class<? extends ServiceFactoryBean> serviceFactoryBeanClass;

	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}

	public void setServiceFactoryBeanClass(Class<? extends ServiceFactoryBean> serviceFactoryBeanClass) {
		this.serviceFactoryBeanClass = serviceFactoryBeanClass;
	}

	/**
	 * 创建一个ClassPathServiceScanner,对我们指定的包路径进行扫描，该类继承自ClassPathBeanDefinitionScanner
	 *
	 * @param registry
	 * @throws BeansException
	 */
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		ClassPathServiceScanner scanner = new ClassPathServiceScanner(registry);
		scanner.setServiceFactoryBeanClass(this.serviceFactoryBeanClass);
		scanner.registerFilters();
		scanner.doScan(basePackage);
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

	}
}
